<?php
/**
 *  
 *
  */
namespace Org\Util;
class Rcqnc {

    
     public function cqnc_balance() {
		$day=date("Y-m-d");
		 $day_time=strtotime($day);
		 $insert_data=array();
		 $lm=array();
		// foreach($open_time as $k=>$v){
// 				  $kj_time[$k]=strtotime($day.' '.$v['kj_time']);
				  $n_time=time(); 
				 // if($n_time>$kj_time[$k]&&$n_time<$kj_time[$k]+60*3){  //根据后台开始将时间之后3分钟内不停的获取开奖数据
				    $condition['c_id']=8;	
					//$condition['add_time'] = array(array('gt',$day_time)) ; 
 					$max_c_no=M('lottery_results')->where($condition)->max('c_no'); //获取当天最大期数
					 
					$url='http://a.apiplus.net/newly.do?token=5ee5aca63461f954&code=cqklsf&rows=1&format=json'; //接口地址
					
					$new_result=get_http($url);
					$result=json_decode($new_result,true);
					//  print_r($result);
					// print_r($result['data'][0]['expect']);exit;
				
					if($max_c_no!=$result['data'][0]['expect']){
				 	// print_r($result['data'][0]['expect']);exit;
					S('cqnc_'.$result['data'][0]['expect'],$result['data'][0]['opencode']);  //存入缓存  方便计算两面长龙算法
					$insert_data['c_no']=$result['data'][0]['expect'];
					$insert_data['c_id']=8;
					$insert_data['c_name']='重庆幸运农场';
					$insert_data['add_time']=$result['data'][0]['opentimestamp'];
					$insert_data['lottery_result']=$result['data'][0]['opencode'];
					$insert_data['status']=0;
					$num= explode(",",$result['data'][0]['opencode']);
				
					$sum=array_sum($num);
					$lm[0]=$sum;
					$lm[1]=$this->zh_ds($sum);
		            $lm[2]=$this->zh_dx($sum);
		            $lm[3]=$this->zh_w_dx($sum);	 
		            $lm[4]=$this->lht($num);	 
				    $n_lm=implode(",",$lm);
					$insert_data['lm']=$n_lm;
					if($max_c_no+1==$result['data'][0]['expect']){  //计算两面长龙结果
					  
					  $bdx=$this->bdx($num); //1-8大小
					  $bds=$this->bds($num); //1-8单双 
					  $bdx_w=$this->bdx_w($num); //1-8合数大小 
					  $bds_h=$this->bds_h($num); //1-8尾单双 
					  $lh=$this->lh($num); //1-4龙虎
 					   $flag=1;
 					 
					     
					    for($i=1;$i<=8;$i++){
						  $strds='flag_bds'.$i;
					      $$strds=0;
						  $strds='flag_bds_h'.$i;
					      $$strds=0;
						   
						  $strdx='flag_bdx'.$i;
					      $$strdx=0;
						  
						  $strdx='flag_bdx_w'.$i;
					      $$strdx=0;
						  
						  $strdx='flag_bdx_w'.$i;
					      $$strdx=0;
						  
						  $str_lh='flag_lh'.$i;
					      $$str_lh=0;
					   }
					   
					     $flag_lht=0;
						 $flag_zds=0;
						 $flag_zdx_w=0;
						 $flag_zdx=0;
					   $k=1;
					   do{
						   
						   $old_lottery=$result['data'][0]['expect']-$k;
						   //S('bjpk10_'.$old_lottery,NULL);
						   $cqnc_result=S('cqnc_'.$old_lottery);
			 
							if(empty($cqnc_result))
							{
							$condition1['c_id']=8;
							$condition1['c_no']=$old_lottery;
							$cqnc_result=M('lottery_results')->where($condition1)->getField('lottery_result');
							if($cqnc_result1){
							S('cqnc_'.$old_lottery,$cqnc_result);
							}else{
								$flag=0;
								}
							}	
						  
 						   $num1[$k-1]= explode(",",$cqnc_result);
						  $sum1[$k-1]=array_sum($num1[$k-1]);
						 $bds1=$this->bds($num1[$k-1]);
						 for($i=1;$i<=8;$i++){
						  $str='flag_bds'.$i;
						  $str1='b_ds'.$i;	
						  switch ($i)
							{
							 case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							 case 6:$gread='第六球';
							 break;
							 case 7:$gread='第七球';
							 break;
							 case 8:$gread='第八球';
							 break;
						
							  default:
 									break;
								
							  }
						   if($$str==0){
							   $ab=$i-1;
							  if($bds1[$ab]==$bds[$ab]){
								 
							   $b=$k+1;
								 if($bds1[$ab]=='双'){
								 $$str1='DS'.$i.'=S,'.$b.','.$gread.'-双';
								 }else{
								 $$str1='DS'.$i.'=D,'.$b.','.$gread.'-单';
									 }
							   }else{
							   $$str=1;	 
								   }
						   }
						  }
						   
						  $bdx1=$this->bdx($num1[$k-1]);
						
						 for($i=1;$i<=8;$i++){
						  $str2='flag_bdx'.$i;
						  $str3='b_dx'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							 case 6:$gread='第六球';
							 break;
							 case 7:$gread='第七球';
							 break;
							 case 8:$gread='第八球';
							 break;
							
							  default:
 									break;
								
							  }
						   if($$str2==0){
							   $ab=$i-1;
							  if($bdx1[$ab]==$bdx[$ab]){
								 
							   $b=$k+1;
								 if($bdx1[$ab]=='小'){
								 $$str3='DX'.$i.'=X,'.$b.','.$gread.'-小';
								 }else{
								 $$str3='DX'.$i.'=D,'.$b.','.$gread.'-大';
									 }
							   }else{
							   $$str2=1;	 
								   }
						   }
						  } 
						 
						   //尾大小
						    $bdx1_w=$this->bdx_w($num1[$k-1]);
					
						 for($i=1;$i<=8;$i++){
						  $str4='flag_bdx_w'.$i;
						  $str5='b_dx_w'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							 case 6:$gread='第六球';
							 break;
							 case 7:$gread='第七球';
							 break;
							 case 8:$gread='第八球';
							 break;
							
							  default:
 									break;
								
							  }
						   if($$str4==0){
							   $ab=$i-1;
							  if($bdx1_w[$ab]==$bdx_w[$ab]){
								 
							   $b=$k+1;
								 if($bdx1_w[$ab]=='尾小'){
								 $$str5='WDX'.$i.'=X,'.$b.','.$gread.'-尾小';
								 }else{
								 $$str5='WDX'.$i.'=D,'.$b.','.$gread.'-尾大';
									 }
							   }else{
							   $$str4=1;	 
								   }
						   }
						  } 
						
						  //合数
						    $bds1_h=$this->bds_h($num1[$k-1]);
						
						 for($i=1;$i<=8;$i++){
						  $str6='flag_bds_h'.$i;
						  $str7='b_ds_h'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 case 5:$gread='第五球';
							 break;
							 case 6:$gread='第六球';
							 break;
							 case 7:$gread='第七球';
							 break;
							 case 8:$gread='第八球';
							 break;
							
							  default:
 									break;
								
							  }
						    if($$str6==0){
							   $ab=$i-1;
							  if($bds1_h[$ab]==$bds_h[$ab]){
								 
							   $b=$k+1;
								 if($bds_h[$ab]=='合双'){
								 $$str7='YHDSHDS'.$i.'=S,'.$b.','.$gread.'-合双';
								 }else{
								 $$str7='YHDS'.$i.'=D,'.$b.','.$gread.'-合单';
									 }
							   }else{
							   $$str6=1;	 
								   }
						   }
						  } 
						  
						    //1-4龙虎
						    $lh1=$this->lh($num1[$k-1]);
						
						 for($i=1;$i<=4;$i++){
						  $str8='flag_lh'.$i;
						  $str9='longhu'.$i;	
						  switch ($i)
							{
							  case 1:$gread='第一球';
							 break;
							 case 2:$gread='第二球';
							 break;
							 case 3:$gread='第三球';
							 break;
							 case 4:$gread='第四球';
							 break;
							 
							
							  default:
 									break;
								
							  }
						    if($$str8==0){
							   $ab=$i-1;
							  if($lh1[$ab]==$lh[$ab]){
								 
							   $b=$k+1;
								 if($lh[$ab]=='龙'){
								 $$str9='LH'.$i.'=L,'.$b.','.$gread.'-龙';
								 }else{
								 $$str9='LH'.$i.'=H,'.$b.','.$gread.'-虎';
									 }
							   }else{
							   $$str8=1;	 
								   }
						   }
						  }
						  
						    //计算总和单双两面长龙
						   $ds1=$this->zh_ds($sum1[$k-1]);
						   
						   
						   if($flag_zds==0){
							   
							  if($ds1==$this->zh_ds($sum)){
							   $b=$k+1;
							   //ZDS=D,2,总和单
								 if($ds1=='双'){
								 $zh_ds='ZDS=S,'.$b.',总和双';
								 }else{
								 $zh_ds='ZDS=D,'.$b.',总和单';
									 }
							   }else{
							   $flag_zds=1;	 
								   }
						   }
						    //计算总和大小两面长龙
						   $dx1=$this->zh_dx($sum1[$k-1]);
						   if($flag_zdx==0){
							   
							  if($dx1==$this->zh_dx($sum)){
							   $b=$k+1;
							   
								 if($dx1=='小'){
								 $zh_dx='ZDX=X,'.$b.',总和小';
								 }else{
										 
										 $zh_dx='ZDX=D,'.$b.',总和大';
									 }
							   }else{
							   $flag_zdx=1;	 
								   }
						   }
						    //计算总和尾大小两面长龙
						   $dx1_w=$this->zh_w_dx($sum1[$k-1]);
						   if($flag_zdx_w==0){
							   
							  if($dx1_w==$this->zh_w_dx($sum)){
							   $b=$k+1;
							  
								 if($dx1_w=='尾小'){
								 $zh_dx_w='ZWDX=X,'.$b.',总和尾数小';
								 }else{
								 $zh_dx_w='ZWDX=D,'.$b.',总和尾数大';
									 }
							   }else{
							   $flag_zdx_w=1;	 
								   }
						   }
						 
						 $k++;	 
					      $sss1='flag_bds1';
						  $sss2='flag_bds2';
						  $sss3='flag_bds3';
						  $sss4='flag_bds4';
						  $sss5='flag_bds5';
						  $sss6='flag_bds6';
						  $sss7='flag_bds7';
						  $sss8='flag_bds8';
						 
						  
						  $xxx1='flag_bdx1';
						  $xxx2='flag_bdx2';
						  $xxx3='flag_bdx3';
						  $xxx4='flag_bdx4';
						  $xxx5='flag_bdx5';
						  $xxx6='flag_bdx6';
						  $xxx7='flag_bdx7';
						  $xxx8='flag_bdx8';
						  
						  $sss1_h='flag_bds_h1';
						  $sss2_h='flag_bds_h2';
						  $sss3_h='flag_bds_h3';
						  $sss4_h='flag_bds_h4';
						  $sss5_h='flag_bds_h5';
						  $sss6_h='flag_bds_h6';
						  $sss7_h='flag_bds_h7';
						  $sss8_h='flag_bds_h8';
						  
						   $xxx1_w='flag_bdx_w1';
						  $xxx2_w='flag_bdx_w2';
						  $xxx3_w='flag_bdx_w3';
						  $xxx4_w='flag_bdx_w4';
						  $xxx5_w='flag_bdx_w5';
						  $xxx6_w='flag_bdx_w6';
						  $xxx7_w='flag_bdx_w7';
						  $xxx8_w='flag_bdx_w8';
						  
						   $xxx1_lh='flag_lh1';
						  $xxx2_lh='flag_lh2';
						  $xxx3_lh='flag_lh3';
						  $xxx4_lh='flag_lh4';
						 
						  if($flag_lht==1&&$flag_zds==1&&$flag_zdx_w==1&&$flag_zdx==1&&$$xxx1==1&&$$xxx2==1&&$$xxx3==1&&$$xxx4==1&&$$xxx5==1&&$$xxx6==1&&$$xxx7==1&&$$xxx8==1&&$$sss1==1&&$$sss2==1&&$$sss3==1&&$$sss4==1&&$$sss5==1&&$$sss6==1&&$$sss7==1&&$$sss8==1&&$$sss1_h==1&&$$sss2_h==1&&$$sss3_h==1&&$$sss4_h==1&&$$sss5_h==1&&$$sss6_h==1&&$$sss7_h==1&&$$sss8_h==1&&$$xxx1_w==1&&$$xxx2_w==1&&$$xxx3_w==1&&$$xxx4_w==1&&$$xxx5_w==1&&$$xxx6_w==1&&$$xxx7_w==1&&$$xxx8_w==1&&$$xxx1_lh==1&&$$xxx2_lh==1&&$$xxx3_lh==1&&$$xxx4_lh==1||$k>10){
							// if($k<11){
							   $flag=0;
							   }
							// $flag=0;  
						   }while($flag==1);
					  
					  
					
					   for($i=1;$i<=8;$i++){
		                 $strx='b_ds'.$i;
			             if($$strx){	 
					     $s[]=$$strx;	
					      }
						  
						  $strbdx='b_dx'.$i;
			              if($$strbdx){	 
					       $s[]=$$strbdx;	
					      }
						  //合数
						    $strbds_h='b_ds_h'.$i;
			              if($$strbds_h){	 
					       $s[]=$$strbds_h;	
					      }
						   //尾大小
						    $strbdx_w='b_dx_w'.$i;
			              if($$strbdx_w){	 
					       $s[]=$$strbdx_w;	
					      }
						  
					   }
					   //1-4龙虎
					   for($i=1;$i<=4;$i++){
						    
						    $strbdx_lh='longhu'.$i;
			              if($$strbdx_lh){	 
					       $s[]=$$strbdx_lh;	
					      }
					   }
					   
						$s[]=$zh_ds;	
						    
					    $s[]=$zh_dx;
						  
					    $s[]=$zh_dx_w;
						
						
						//print_r($s);exit;
						 
					   $n_s=implode(";",$s);	
						
					  $insert_data['detail']=$n_s;	
						}
					  
					$insert_result=M('lottery_results')->data($insert_data)->add();	
				
				 	if(!$insert_result){
						 
						 file_put_contents("test.txt", date("Y-m-d H:i:s") . "获取重庆时时彩结果存入失败！" . "\r\n<br>", FILE_APPEND);  	
							}else{
						$cqnc_result = new \Org\Util\Jscqnc(); //重庆农场
        $cqnc_result->cqnc_balance();
					}
 					}
					 
		return; 		
     }
	
 public function zh_ds($sum){  //总和单双
      if($sum%2==0){
		$ds='双';
			}else{
		$ds='单';	 
		}
		return $ds;
 }
 public function zh_dx($sum){  //总和大小
      if($sum>84){
		   $dx='大';	 
			 }elseif($sum==84){
				$dx='和';	 
			 }else{
			 $dx='小';	 
				 }
		return $dx;
 }
  public function zh_w_dx($sum){  //总和尾大小
     $w=substr($sum,-1);

	 if($w>=5){
		   $dx='尾大';	 
			 }else{
			 $dx='尾小';	 
				 }
		return $dx;
 }
 
 public function lht($num){  //总和龙虎
 foreach($num as $k => $v){
						$num[$k]=preg_replace('/^0+/','',$v);   //去除左边的0
						}
      if($num[0]>$num[7]){
			$lht='龙';	 
			 }elseif($num[0]<$num[7]){
				$lht='虎';	  
				 }	else{
				$lht='和';	  	 
					 }	
		return $lht;
 }
 //1-8龙虎比较
  public function lh($num){  //总和龙虎
  if(empty($num)){
	return $lh;  
  }
  	foreach($num as $k => $v){
						$num[$k]=preg_replace('/^0+/','',$v);   //去除左边的0
						}
  $lh=array();
      if($num[0]<$num[7]){
			$lht='虎';
			
			
			 }else{
				$lht='龙';	  
				 }
		$lh[0]=$lht;
		 if($num[1]>$num[6]){
			$lht='龙';	 
			 }else{
				$lht='虎';	  
				 }
		$lh[1]=$lht;
		 if($num[2]>$num[5]){
			$lht='龙';	 
			 }else{
				$lht='虎';	  
				 }
		$lh[2]=$lht;
		 if($num[3]>$num[4]){
			$lht='龙';	 
			 }else{
				$lht='虎';	  
				 }
		$lh[3]=$lht;
		
		return $lh;
 }
 public function bdx($num){  //1-8球大小
	 $bdx=array();
	 foreach($num as $k=>$v){
		 if($v>=11){
		   $bdx[$k]='大';	 
			 }else{
			$bdx[$k]='小';	 
				 }
 		 }
	return $bdx;	 
  }
   public function bdx_w($num){  //1-8球尾大小
	 $wdx=array();
	 foreach($num as $k=>$v){
		  $w=substr($v,-1);

	 if($w>4){
		   $wdx[$k]='尾大';	 
			 }else{
			 $wdx[$k]='尾小';	 
				 }
		
 		 }
	return $wdx;	 
  }
 public function bds($num){  //1-8球单双
	 $bds=array();
	 foreach($num as $k=>$v){
		 if($v%2==0){
		   $bds[$k]='双';	 
			 }else{
			$bds[$k]='单';	 
				 }
 		 }
	return $bds;	 
  }	 
 public function bds_h($num){  //1-8球合数单双
	 $bds=array();
	 foreach($num as $k=>$v){
		 //个位
		 $g=$v%10;
		 $shi=$v/10%10;
		 $sum=$g+$shi;
		 if($sum%2==0){
		   $bds[$k]='合双';	 
			 }else{
			$bds[$k]='合单';	 
				 }
 		 }
	return $bds;	 
  }
	 
}